package org.minos.edit.model
{
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import flash.display.Loader;
	import flash.filesystem.File;
	import org.minos.edit.EditFacate;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	/**
	 * ...
	 * @author M.ichow
	 */
	public class AssetsProxy extends Proxy
	{
		public static const NAME : String = "AssetsProxy";
		private var resType : Array = [ ".swf" , ".mp3" ];
		
		public function AssetsProxy( data : Object = null )
		{
			super( NAME , data );
		}
		
		public function getAssets( path : String ) : void
		{
			var file : File = new File( path );
			var files : Array = getDirFiles( file );
			
			var list : Array = [];
			
			var _max : LoaderMax = new LoaderMax();
			
			for each ( var i : File in files )
			{
				var obj : Object = {};
				obj.label = i.name;
				obj.url = file.getRelativePath( i , true );
				list.push( obj );
			}
			
			sendNotification( EditFacate.ASSETS_CHANGE , list );
		}
		
		private function getDirFiles( dir : File ) : Array
		{
			var arr : Array = [];
			var dirFiles : Array = dir.getDirectoryListing();
			var file : File;
			
			for ( var i : int = 0 ; i < dirFiles.length ; i++ )
			{
				file = dirFiles[ i ];
				if ( file.isDirectory )
				{
					arr = arr.concat( getDirFiles( file ) );
					continue;
				}
				if ( isResFile( file ) )
				{
					arr.push( file );
				}
			}
			return arr;
		}
		
		private function isResFile( file : File ) : Boolean
		{
			for ( var i : uint = 0 ; i < resType.length ; i++ )
			{
				if ( file.name.toLowerCase().indexOf( resType[ i ] ) != -1 )
				{
					return true;
				}
			}
			return false;
		}
	
	}

}